function MainCtrl(user) {
    this.user = user;
}
MainCtrl.$inject = ['user'];

function SearchFormCtrl($location) {
	this.sub = function() {
		$location.hashPath = 'search';
        $location.hashSearch = {q: this.search};
	}
}
SearchFormCtrl.$inject = ['$location'];

function SearchCtrl($xhr) {
    this.search = this.params.q;
    var scope = this;
    $.getJSON('/search/' + this.search, function(data) {
        scope.data = data;
        scope.$root.$eval();
    });
}
SearchCtrl.$inject = ['$xhr'];

function SignupCtrl($xhr, $resource, user, $location) {
    if (user.name)
        $location.updateHash('');

	var scope = this;
	$xhr('GET', '/security/signupform', function (code, data) {
		scope.data = data;
	});

	this.res = $resource('/security/signup');

	this.signup = function() {
		this.dont_match = this.password != this.password2;
		if (!this.dont_match) {
			scope.data = this.res.get({
				'name': this.name,
				'password': this.password,
				'capchatext': this.capchatext,
				'capchaid': this.data.capchaid
			}, function(data) {
				if (angular.isDefined(data.success)) {
                    user.name = scope.name;
                    $location.updateHash('');
                    $("#playlist .label").show();
				}
			});

		}
	}
}
SignupCtrl.$inject = ['$xhr', '$resource', 'user', '$location'];

function LoginCtrl($xhr, $location, user) {

	this.login = function(){
		var scope = this;
		$xhr('GET', '/security/login?name=' + this.name + '&password=' + this.password,
		function (code, data) {
			scope.nameerr = data.nameerr;
			scope.passworderr = data.passworderr;
			if (typeof data.success !== 'undefined') {
				user.name = scope.name;
                $location.updateHash('');
                $("#playlist .label").show();
			}
		}, function (code, data) {
			//
		});
	}
}
LogoutCtrl.$inject = ['$xhr', '$location', 'user'];

function LogoutCtrl($xhr, $location, user) {
	$xhr('GET', '/security/logout', function (code, data) {
        user.name = '';
        $location.updateHash('');
        $("#playlist .label").hide();
		//window.location.replace("/");
	})
}
LogoutCtrl.$inject = ['$xhr', '$location', 'user'];

function ArtistCtrl() {
    var self = this;
    $.getJSON('/artist/' + this.params.id, function(data) {
        self.data = data;
        self.$root.$eval();
    });
}

function ReleaseCtrl() {
    var self = this;
    $.getJSON('/release/' + this.params.id, function(data) {
        self.data = data;
        self.$root.$eval();
    });
}

function LabelCtrl() {
    var self = this;
    $.getJSON('/label/' + this.params.id, function(data) {
        self.data = data;
        self.$root.$eval();
    });
}

function ArtistSearchCtrl() {
    var self = this;
    self.query = this.params.query;
    $.getJSON('/search/artist/' + this.params.query + '?page=' + this.params.page, function(data) {
        self.data = data;
        self.$root.$eval();
    });
}

function LabelSearchCtrl() {
    var self = this;
    self.query = this.params.query;
    $.getJSON('/search/label/' + this.params.query + '?page=' + this.params.page, function(data) {
        self.data = data;
        self.$root.$eval();
    });
}

function ReleaseSearchCtrl() {
    var self = this;
    self.query = this.params.query;
    $.getJSON('/search/release/' + this.params.query + '?page=' + this.params.page, function(data) {
        self.data = data;
        self.$root.$eval();
    });
}

function TrackSearchCtrl() {
    var self = this;
    self.query = this.params.query;
    $.getJSON('/search/track/' + this.params.query + '?page=' + this.params.page, function(data) {
        self.data = data;
        self.$root.$eval();
    });
}

function MyPlaylistsCtrl(xhr) {
    var self = this;
    $.getJSON('/myplaylists/', function(data) {
        self.data = data;
        self.$root.$eval();
    });

    this.deletepl = function(pl) {
        var self = this;
        xhr('DELETE', '/playlists/' + pl.id, function(data, d) {
            pl.delete_text = d;
            var idx = self.data.indexOf(pl);
            if (d == "ok")
                self.data.splice( idx, 1);
            else
                pl.delete_text = 'error'
        });
    }
}
MyPlaylistsCtrl.$inject = ["$xhr"];

function PlaylistCtrl() {
    var self = this;
    self.id = this.params.id;
    $.getJSON('/playlists/' + self.id, function(data) {
        self.data = data;
        self.tracklist = {
            tracks: data.tracks
        };
        self.$root.$eval();
    });
}

function SavePlaylistCtrl($location) {
    var self = this;

    this.save = function() {
        if (this.playlist_name === '') {
            this.empty = true;
            this.success = false;
        } else {
            this.empty = false;
            var tracks = [];
            $('#playlist li.draggable').each(function (index, el) {
                tracks.push({ id: $(el).attr('data-trackid') });
            });
            console.log(self.playlist_name);
            var tracks_string = '';
            for (var i = 0; i < tracks.length; i++) {
                tracks_string += '&p.tracks[' + i + '].id=' + tracks[i].id;
            }
            $.post('/playlists/?p.name=' + self.playlist_name + tracks_string, function(data) {
                self.success = true;
                var port = $location.port == 80 ? '' : ':' + $location.port;
                self.share = $location.host + port + '/#playlist/' + data;
                self.$root.$eval();
            });
        }
    }
}
SavePlaylistCtrl.$inject = ['$location'];

function MostLikedCtrl() {
    var self = this;
    self.page = this.params.page;
    $.getJSON('/mostliked/' + self.page, function(data) {
        self.data = data;
        self.tracklist = {
            tracks: data
        };
        self.$root.$eval();
    });
}

function MostDislikedCtrl() {
    var self = this;
    self.page = this.params.page;
    $.getJSON('/mostdisliked/' + self.page, function(data) {
        self.data = data;
        self.tracklist = {
            tracks: data
        };
        self.$root.$eval();
    });
}

function IndexCtrl() {

}